package com.aidong.dynamic.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.beans.factory.annotation.Autowired;

import com.aidong.dynamic.biz.TrnDynamicMessageBiz;
import com.aidong.dynamic.entity.TrnDynamicContent;
import com.aidong.dynamic.entity.TrnMessageCount;
import com.aidong.util.PageUtil;
import com.aidong.util.ReadPropertiesUtil;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 运动圈消息列表
 * 
 * @author user
 *
 */
@ParentPackage("json")
@Namespace("/")
public class TrnDynamicMessageAction implements ModelDriven<TrnDynamicContent> {

	private TrnDynamicContent trnDynamicContent; // 运动圈entity

	@Override
	public TrnDynamicContent getModel() {
		if (trnDynamicContent == null) {
			trnDynamicContent = new TrnDynamicContent();
		}
		return trnDynamicContent;
	}

	private TrnDynamicMessageBiz messageBiz;

	@Autowired
	public void setMessageBiz(TrnDynamicMessageBiz messageBiz) {
		this.messageBiz = messageBiz;
	}

	private Map<String, Object> map; // 返回的json数据类型

	private int pageCurrent; // 当前页 工具属性 用来分页

	/**
	 * 当前页 工具属性 用来分页
	 * 
	 * @param pageCurrent
	 */
	public void setPageCurrent(int pageCurrent) {
		this.pageCurrent = pageCurrent;
	}

	/**
	 * 返回的json数据类型
	 * 
	 * @return
	 */
	@JSON
	public Map<String, Object> getMap() {
		return map;
	}

	/**
	 * 运动圈消息列表
	 * 
	 * @return
	 */
	@Action(value = "getmessagepush", results = { @Result(name = "succ", type = "json", params = { "root", "map" }) })
	public String getMessagePush() {
		map = new HashMap<String, Object>();
		try {
			int idongId = trnDynamicContent.getIdongId();
			if (idongId > 0) {
				Map<String, Object> pageMap = PageUtil.page(10, pageCurrent);
				pageMap.put("idongId", idongId);
				List<Map<String, Object>> pushs = messageBiz.getMessagePush(pageMap);
				if (pushs.size() > 0) {
					for (Map<String, Object> push : pushs) {
						push.put("headUrl", ReadPropertiesUtil.getIMGREAD(push.get("headUrl")));
						push.put("picUrl", ReadPropertiesUtil.getIMGREAD(push.get("picUrl")));
					}
				}

				map.put("code", 1);
				map.put("pageCurrent", pageMap.get("pageCurrent"));
				map.put("result", pushs);
				return "succ";
			} else {
				map.put("code", 0);
				map.put("msg", "参数缺失");
				return "succ";
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
	}

	/**
	 * 消息列表单条删除
	 * 
	 * @return
	 */
	@Action(value = "delmessagelistone", results = {
			@Result(name = "succ", type = "json", params = { "root", "map" }) })
	public String delMessageListOne() {

		map = new HashMap<String, Object>();

		if (trnDynamicContent.getMessageID() > 0) {
			Map<String, Object> paraMap = new HashMap<String, Object>();
			paraMap.put("id", trnDynamicContent.getMessageID());
			try {
				messageBiz.delMessageListOne(paraMap);
				// 删除成功
				map.put("code", 1);
				map.put("result", "删除成功");
				return "succ";
			} catch (Exception e) {
				map.put("code", 0);
				map.put("msg", "系统异常");
				e.printStackTrace();
				return "succ";
			}

		} else {
			map.put("code", 0);
			map.put("msg", "参数缺失"); // 删除失败
			return "succ";
		}
	}

	/**
	 * 消息列表清空
	 * 
	 * @return
	 */
	@Action(value = "delmessagelistall", results = {
			@Result(name = "succ", type = "json", params = { "root", "map" }) })
	public String delMessageListAll() {

		map = new HashMap<String, Object>();

		if (trnDynamicContent.getIdongId() > 0) {
			Map<String, Object> paraMap = new HashMap<String, Object>();
			paraMap.put("idongId", trnDynamicContent.getIdongId());
			try {
				messageBiz.delMessageListAll(paraMap);
				// 删除成功
				map.put("code", 1);
				map.put("result", "删除成功");
				return "succ";
			} catch (Exception e) {
				map.put("code", 0);
				map.put("msg", "系统异常");
				e.printStackTrace();
				return "succ";
			}

		} else {
			map.put("code", 0);
			map.put("msg", "参数缺失"); // 删除失败
			return "succ";
		}
	}

	/**
	 * 消息列表数量提醒
	 * 
	 * @return
	 */
	@Action(value = "getmessagecount", results = { @Result(name = "succ", type = "json", params = { "root", "map" }) })
	public String getMessageCount() {
		map = new HashMap<String, Object>();
		if (trnDynamicContent.getIdongId() > 0) {
			Map<String, Object> paraMap = new HashMap<String, Object>();
			paraMap.put("idongId", trnDynamicContent.getIdongId());
			try {
				TrnMessageCount messageCount = messageBiz.getMessageCount(paraMap);
				map.put("code", 1);
				map.put("result", messageCount);
				return "succ";
			} catch (Exception e) {
				e.printStackTrace();
				map.put("code", 0);
				map.put("msg", "系统异常");
				return "succ";
			}

		} else {
			map.put("code", 0);
			map.put("msg", "参数缺失"); // 删除失败
			return "succ";
		}
	}

	/**
	 * 运动圈消息查看状态改变
	 * 
	 * @return
	 */
	@Action(value = "editmessagestatus", results = {
			@Result(name = "succ", type = "json", params = { "root", "map" }) })
	public String editMessageStatus() {

		map = new HashMap<String, Object>();

		if (trnDynamicContent.getIdongId() > 0) {
			Map<String, Object> paraMap = new HashMap<String, Object>();
			paraMap.put("idongId", trnDynamicContent.getIdongId());
			try {
				messageBiz.editMessageStatus(paraMap);
				return null;
			} catch (Exception e) {
				e.printStackTrace();
				map.put("code", 0);
				map.put("msg", "系统异常");
				return "succ";
			}

		} else {
			map.put("code", 0);
			map.put("msg", "参数缺失"); // 删除失败
			return "succ";
		}
	}

	/**
	 * 未读消息
	 * 
	 * @return
	 */
	@Action(value = "getunreadmessage", results = { @Result(name = "succ", type = "json", params = { "root", "map" }) })
	public String getUnreadMessages() {

		map = new HashMap<String, Object>();

		if (trnDynamicContent.getIdongId() > 0) {
			Map<String, Object> paraMap = new HashMap<String, Object>();
			paraMap.put("idongId", trnDynamicContent.getIdongId());
			try {
				List<Map<String, Object>> unreads = messageBiz.getUnreadMessages(paraMap);
				if (unreads.size() > 0) {
					for (Map<String, Object> unread : unreads) {
						unread.put("headUrl", ReadPropertiesUtil.getIMGREAD(unread.get("headUrl")));
						unread.put("picUrl", ReadPropertiesUtil.getIMGREAD(unread.get("picUrl")));
					}
				}

				map.put("code", 1);
				map.put("result", unreads);
				return "succ";
			} catch (Exception e) {
				e.printStackTrace();
				map.put("code", 0);
				map.put("msg", "系统异常");
				return "succ";
			}

		} else {
			map.put("code", 0);
			map.put("msg", "参数缺失"); // 删除失败
			return "succ";
		}
	}

	/**
	 * 已读消息
	 * 
	 * @return
	 */
	@Action(value = "getreadmessage", results = { @Result(name = "succ", type = "json", params = { "root", "map" }) })
	public String getreadMessages() {

		map = new HashMap<String, Object>();

		if (trnDynamicContent.getIdongId() > 0) {
			Map<String, Object> pageMap = PageUtil.page(10, pageCurrent);
			pageMap.put("idongId", trnDynamicContent.getIdongId());
			try {
				List<Map<String, Object>> unreads = messageBiz.getreadMessages(pageMap);
				if (unreads.size() > 0) {
					for (Map<String, Object> unread : unreads) {
						unread.put("headUrl", ReadPropertiesUtil.getIMGREAD(unread.get("headUrl")));
						unread.put("picUrl", ReadPropertiesUtil.getIMGREAD(unread.get("picUrl")));
					}
				}

				map.put("code", 1);
				map.put("pageCurrent", pageMap.get("pageCurrent"));
				map.put("result", unreads);
				return "succ";
			} catch (Exception e) {
				e.printStackTrace();
				map.put("code", 0);
				map.put("msg", "系统异常");
				return "succ";
			}

		} else {
			map.put("code", 0);
			map.put("msg", "参数缺失"); // 删除失败
			return "succ";
		}
	}

	/**
	 * 查看动态或评论是否存在
	 * 
	 * @return
	 */
	@Action(value = "getifdynamicexit", results = { @Result(name = "succ", type = "json", params = { "root", "map" }) })
	public String getIfDynamicExit() {
		map = new HashMap<String, Object>();

		int dynamicId = trnDynamicContent.getDynamicId();
		int infoId = trnDynamicContent.getInfoId();
		if (dynamicId > 0 || infoId > 0) {
			try {
				Map<String, Object> paraMap = new HashMap<String, Object>();
				paraMap.put("dynamicId", dynamicId);
				paraMap.put("infoId", infoId);
				int dynamicCount = messageBiz.getDynamicExit(paraMap);
				int infoCount = messageBiz.getCommentExit(paraMap);

				if (dynamicCount == 0) {
					map.put("code", 1);
					map.put("result", false);
					return "succ";
				}
				if (infoCount == 0) {
					map.put("code", 1);
					map.put("result", false);
					return "succ";
				}

				map.put("code", 1);
				map.put("result", true);
				return "succ";
			} catch (Exception e) {
				e.printStackTrace();
				map.put("code", 0);
				map.put("msg", "系统异常");
				return "succ";
			}
		} else {
			map.put("code", 0);
			map.put("msg", "参数缺失");
			return "succ";
		}
	}
}